package models

import (
	"strconv"
	"strings"

	"gitee.com/tsingeye/FreeEhome/config"
	"gitee.com/tsingeye/FreeEhome/service/ehome/udp"
	"gitee.com/tsingeye/FreeEhome/tools"
	"gitee.com/tsingeye/FreeEhome/tools/logs"
	"gitee.com/tsingeye/FreeEhome/tools/sqlDB"
)

//查询指定通道录像文件
func QueryRecordedFiles(channelID string, fileType int64, startTime, stopTime string) (replyData map[string]interface{}) {
	replyData = map[string]interface{}{
		"errCode": config.EHomeClientBadRequest,
		"errMsg":  config.HTTPCodeMap[config.EHomeClientBadRequest],
	}

	strList := strings.Split(channelID, "_")
	if len(strList) != 2 {
		logs.BeeLogger.Warn("RecordController.QueryRecordedFiles() ---> channelID=%s, channelID format error!!!", channelID)
		return
	}

	realChannelID, err := strconv.ParseInt(strList[1], 0, 64)
	if err != nil {
		logs.BeeLogger.Warn("RecordController.QueryRecordedFiles() ---> channelID=%s, channelID format error!!!", channelID)
		return
	}

	//校验录像类型
	switch fileType {
	case 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 101, 102, 103, 104:
	default:
		logs.BeeLogger.Error("RecordController.QueryRecordedFiles() ---> channelID=%s, fileType=%d, unknown fileType!!!", channelID, fileType)
		return
	}

	//校验开始时间
	if !tools.CheckTimeFormat(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, startTime) {
		logs.BeeLogger.Error("RecordController.QueryRecordedFiles() ---> channelID=%s, startTime=%s, invalid time format!!!", channelID, startTime)
	}
	//校验结束时间
	if !tools.CheckTimeFormat(`^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$`, stopTime) {
		logs.BeeLogger.Error("RecordController.QueryRecordedFiles() ---> channelID=%s, stopTime=%s, invalid time format!!!", channelID, stopTime)
	}

	if startTime >= stopTime {
		logs.BeeLogger.Error("RecordController.QueryRecordedFiles() ---> channelID=%s, startTime=%s, stopTime=%s, startTime>=stopTime, parameter error!!!", channelID, startTime, stopTime)
		return
	}

	var channelData sqlDB.ChannelTable
	if err := sqlDB.First(&channelData, "channelID = ?", channelID); err != nil {
		logs.BeeLogger.Error("RecordController.QueryRecordedFiles() ---> channelID=%s, sqlDB.First(&sqlDB.ChannelTable{}) error:%s", channelID, err)

		replyData = map[string]interface{}{
			"errCode": config.EHomeChannelIDNotFound,
			"errMsg":  config.HTTPCodeMap[config.EHomeChannelIDNotFound],
		}
		return
	}

	return udp.QueryRecordedFiles(channelData.DeviceID, realChannelID, fileType, startTime, stopTime)
}

//指定通道录像文件播放
func UploadFile(channelID, filename string) (replyData map[string]interface{}) {
	replyData = map[string]interface{}{
		"errCode": config.EHomeClientBadRequest,
		"errMsg":  config.HTTPCodeMap[config.EHomeClientBadRequest],
	}

	if filename == "" {
		return
	}

	strList := strings.Split(channelID, "_")
	if len(strList) != 2 {
		logs.BeeLogger.Warn("RecordController.UploadFile() ---> channelID=%s, channelID format error!!!", channelID)
		return
	}

	realChannelID, err := strconv.ParseInt(strList[1], 0, 64)
	if err != nil {
		logs.BeeLogger.Warn("RecordController.UploadFile() ---> channelID=%s, channelID format error!!!", channelID)
		return
	}

	var channelData sqlDB.ChannelTable
	if err := sqlDB.First(&channelData, "channelID = ?", channelID); err != nil {
		logs.BeeLogger.Error("RecordController.UploadFile() ---> channelID=%s, sqlDB.First(&sqlDB.ChannelTable{}) error:%s", channelID, err)

		replyData = map[string]interface{}{
			"errCode": config.EHomeChannelIDNotFound,
			"errMsg":  config.HTTPCodeMap[config.EHomeChannelIDNotFound],
		}
		return
	}

	return udp.UploadFile(channelData.DeviceID, realChannelID, filename)
}
